package beautiful.butterfly.drds.binlog.utils;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

@Slf4j
public class PropertiesExtend
{


    // 以下为全局需要

    private static PropertiesExtend propertiesExtend = null;// 全局单例变量，一开始就存在

    static
    {// 静态块里，只加载一次

        Properties properties = new Properties();
        try
        {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(Constants.config_file));
            properties.load(inputStream);
            inputStream.close();
        } catch (Exception e)
        {
            // logger.error(e.toString());
            log.info("fail to read config file " + Constants.config_file);
            System.exit(-1);
        }
        // 读取值mysql
        log.info("succeed to read config file " + Constants.config_file);
        // netty
        int netty_server_port = Integer.parseInt(properties.getProperty(Constants.netty_server_port, "10000"));
        int netty_boss_number = Integer.parseInt(properties.getProperty(Constants.netty_boss_number, "1"));
        int netty_worker = Runtime.getRuntime().availableProcessors()
                * Integer.parseInt(properties.getProperty(Constants.netty_worker_number, "2").trim());// 2倍cpu
        // consumer
        int consumer_worker = Runtime.getRuntime().availableProcessors()
                * Integer.parseInt(properties.getProperty(Constants.consumer_worker_number, "2").trim());// 2倍cpu
        // zk
        String zk_servers = properties.getProperty(Constants.zk_servers);
        // countvalve
        long countValve = Long.parseLong(properties.getProperty(Constants.count_valve, "100"));
        properties = null;
        // 构造新的对象
        propertiesExtend = new PropertiesExtend(netty_server_port, netty_boss_number, netty_worker, consumer_worker, zk_servers, countValve);
        log.info("succeed to create my properties object ");
    }

    // 私有属性开始//////////////////////////////////////////////////////////////////
    // netty
    @Setter
    @Getter
    private int nettyPort;
    @Setter
    @Getter
    private int nettyBoss;
    @Setter
    @Getter
    private int nettyWorker;
    // consumer worker
    @Setter
    @Getter
    private int consumerWorker;
    // zk
    @Setter
    @Getter
    private String zk_servers;
    //
    @Setter
    @Getter
    private long accumalatedCountValue;

    private PropertiesExtend()
    {// 私有方法，保证单例

    }

    private PropertiesExtend(int nettyPort, int nettyBoss, int nettyWorker, int consumerWorker, String zk_servers, long countValue)
    {

        // used by netty
        this.nettyPort = nettyPort;
        this.nettyBoss = nettyBoss;
        this.nettyWorker = nettyWorker;
        this.consumerWorker = consumerWorker;
        this.zk_servers = zk_servers;
        this.accumalatedCountValue = countValue;

    }

    public static PropertiesExtend getInstance()
    {
        return propertiesExtend;
    }


}
